﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace PPI.Data.Inspection
{
    public class DInspectionResult
    {
        public static DataSet SelectAll(Int32 resultId,string connectionKey)
        {
            SqlDataAdapter da = null;
            try
            {
                da = new SqlDataAdapter("up_Inspection_Results_SelectAllBYInspectionResult", DBHelper.GetConnection(connectionKey));
                da.SelectCommand.CommandType = CommandType.StoredProcedure;

                #region Result

                if(resultId == Int32.MinValue)
                {
                    da.SelectCommand.Parameters.Add(
                        DBHelper.CreateIntParameter("@InspectionResultId", ParameterDirection.Input, true, DBNull.Value));
                }else
                {
                    da.SelectCommand.Parameters.Add(
    DBHelper.CreateIntParameter("@InspectionResultId", ParameterDirection.Input, false, resultId));

                }

                #endregion

                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                throw new Exception("Error executing up_Inspection_Images_SelectAll", ex);
            }
            finally
            {
                if (da != null) da.SelectCommand.Connection.Close();
            }
        }

        public static DataSet SelectByInspection(Int32 inspectionId, string connectionKey)
        {
            SqlDataAdapter da = null;
            try
            {
                da = new SqlDataAdapter("up_Inspection_Results_SelectAllBYInspection", DBHelper.GetConnection(connectionKey));
                da.SelectCommand.CommandType = CommandType.StoredProcedure;

                #region Inspection

                if (inspectionId == Int32.MinValue)
                {
                    da.SelectCommand.Parameters.Add(
                        DBHelper.CreateIntParameter("@inspectionFk", ParameterDirection.Input, true, DBNull.Value));
                }
                else
                {
                    da.SelectCommand.Parameters.Add(
    DBHelper.CreateIntParameter("@inspectionFk", ParameterDirection.Input, false, inspectionId));

                }

                #endregion

                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                throw new Exception("Error executing up_Inspection_Results_SelectAllBYInspection", ex);
            }
            finally
            {
                if (da != null) da.SelectCommand.Connection.Close();
            }
        }

        public static int Insert(Int32 InspectionFk, Int32 typeFk,Int32 resultTypeFk,string lastInspection,string comments,bool isElectrical, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;
            try
            {
                cmd = new SqlCommand("up_Inspection_Results_Insert", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;

                cmd.CommandType = CommandType.StoredProcedure;

                #region lastInspection
                if (string.IsNullOrEmpty(lastInspection))
                {
                    cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@LastInspection", 50,ParameterDirection.Input, true, DBNull.Value));
                }else
                {
                    cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@LastInspection", 50, ParameterDirection.Input, true, lastInspection));
                }
                #endregion

                #region Comments
                if (string.IsNullOrEmpty(comments))
                {
                    cmd.Parameters.Add(DBHelper.CreateTextParameter("@Comments", ParameterDirection.Input, true, DBNull.Value));
                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateTextParameter("@Comments", ParameterDirection.Input, false, comments));
                }
                #endregion


                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_InspVisit_XmissionStructures_Inspection_TypeFk", ParameterDirection.Input, false, typeFk));
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_InspVisit_XmissionStructures_ResultTypeFk", ParameterDirection.Input, false, resultTypeFk));
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_InspVisit_XMissionStructure_PoleFk", ParameterDirection.Input, false, InspectionFk));
                cmd.Parameters.Add(DBHelper.CreateBitParameter("@IsElectrical", ParameterDirection.Input, false, isElectrical));

                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_InspVisit_XmissionStructures_ResultId", ParameterDirection.ReturnValue, false, 0));
                cmd.ExecuteNonQuery();
                tr.Commit();
                return (Int32)cmd.Parameters["@Vendor_InspVisit_XmissionStructures_ResultId"].Value;
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();
                throw  ex;
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }

        public static void Delete(Int32 inspectionResultId, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;
            try
            {
                cmd = new SqlCommand("up_Inspection_Results_Delete", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;

                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@InspectionResultId", ParameterDirection.Input, false, inspectionResultId));

                cmd.ExecuteNonQuery();
                tr.Commit();
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();
                throw ex;
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }

        }

        public static void Update(Int32 inspectionResultId,Int32 InspectionFk, Int32 typeFk,Int32 resultTypeFk,string lastInspection,string comments,bool isElectrical, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;
            try
            {
                cmd = new SqlCommand("up_Inspection_Results_Update", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;

                cmd.CommandType = CommandType.StoredProcedure;

                #region lastInspection
                if (string.IsNullOrEmpty(lastInspection))
                {
                    cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@LastInspection", 50, ParameterDirection.Input, true, DBNull.Value));
                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@LastInspection", 50, ParameterDirection.Input, true, lastInspection));
                }
                #endregion

                #region Comments
                if (string.IsNullOrEmpty(comments))
                {
                    cmd.Parameters.Add(DBHelper.CreateTextParameter("@Comments", ParameterDirection.Input, true, DBNull.Value));
                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateTextParameter("@Comments", ParameterDirection.Input, false, comments));
                }
                #endregion


                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_InspVisit_XmissionStructures_Inspection_TypeFk", ParameterDirection.Input, false, typeFk));
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_InspVisit_XmissionStructures_ResultTypeFk", ParameterDirection.Input, false, resultTypeFk));
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_InspVisit_XMissionStructure_PoleFk", ParameterDirection.Input, false, InspectionFk));
                cmd.Parameters.Add(DBHelper.CreateBitParameter("@IsElectrical", ParameterDirection.Input, false, isElectrical));

                cmd.Parameters.Add(DBHelper.CreateIntParameter("@InspectionResultId", ParameterDirection.Input, false, inspectionResultId));
                cmd.ExecuteNonQuery();
                tr.Commit();
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();
                throw ex;
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }
    }
}
